import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";
import { MessageBox } from "element-ui";
import { getToken } from "@/utils/auth";

Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
/**
 * requiresAuth 隐式路由
 * */
const routes = [
  {
    name: "account",
    path: "/account",
    component: () =>
      import(/* webpackChunkName: 'account' */ "@/views/account"),
    meta: {
      title: "收益管理系统",
    },
  },
  {
    name: "index",
    path: "/",
    redirect: "/home",
    component: () => import(/* webpackChunkName: 'index' */ "@/views/index"),
    meta: { title: "收益管理系统" },
    children: [
      {
        name: "home",
        path: "/home",
        component: () => import(/* webpackChunkName: 'home' */ "@/views/home/index.vue"),
        meta: { title: "首页", root: true },
      },
      {
        name: "warningCase",
        path: "/warningcase",
        component: () => import(/* webpackChunkName: 'warningcase' */ "@/views/warningcase/index.vue"),
        meta: { title: "预警方案", root: true },   // requiresAuth: true,routerTwo:true, requiresPath:'/workbenches' 
      },
    ],
  },
  {
    name: "404",
    path: "/404",
    component: () => import(/* webpackChunkName: '404' */ "@/views/Erroe"),
    meta: {
      title: "找不到网页",
    },
  },
];

const router = new VueRouter({
  mode: 'history',
  base:'/revenuemanage/',
  routes,
});
router.beforeEach(async (to, from, next) => {
  document.title = to.meta.title;

  if (to.name !== "404") {
    if (to.meta.root) {
      store.commit("setBreadList", [
        {
          url: to.name,
          title: to.meta.title,
        },
      ]);
    } else {
      store.commit("addBread", {
        url: to.name,
        title: to.meta.title,
        query: to.query,
      });
      let breadList = store.state.breadList;
      let arr = [];
      for (let i = 0; i < breadList.length; i++) {
        arr.push(breadList[i]);
        if (breadList[i].url === to.name) {
          store.commit("setBreadList", arr);
          breadList = arr;
          break;
        }
      }
    }
  }

  const token = getToken();
  let power = store.state.powerList.indexOf(to.path) !== -1;

  if (token) {
    if (to.path === "/account") {
      next({ path: store.state.powerList[1] });
    } else {
      // 获取菜单
      if (store.state.AsideMenu.length === 0) {
        await store.dispatch("getMenus");
        //在成功获取Menus之后powerList才会有值
        power = store.state.powerList.indexOf(to.path) !== -1;
      }
      if (!power) {
        next({ path: "/404" });
      } else {
        next();
      }
    }
  } else {
    if (to.path === "/account") {
      next();
    } else {
      MessageBox.confirm(
        "登录已过期，请您再次从选住系统云平台登录跳转",
        "提示",
        {
          confirmButtonText: "确定",
          showClose: false,
          showCancelButton: false,
          closeOnClickModal: false,
          type: "warning",
        }
      ).then(() => {
        window.opener = null;
        window.open("about:blank", "_self");
        window.close();
        store.dispatch("logoUp");
      });
    }
  }
});
export default router;
